package com.itheima.web;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Category;
import com.itheima.common.R;
import com.itheima.common.pageParm;
import com.itheima.service.CategoryService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/category")
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R add(@RequestBody Category category) {

        int row = categoryService.add(category);

        if (row > 0) {
            return R.success("添加成功!");
        } else {
            return R.error("添加失败！");
        }


    }

    @GetMapping("/page")
    public R findPage(pageParm pageParm) {
        IPage<Category> page = categoryService.findPage(pageParm);

        return R.success(page);

    }

    @DeleteMapping("/{id}/{type}")
    public R delete(@PathVariable long id, @PathVariable Integer type) {

        int row = categoryService.delete(id, type);
        if (row > 0) {
            return R.success("删除成功！");
        } else {
            return R.error("删除失败！");
        }
    }

    @PutMapping
    public R update(@RequestBody Category category) {

        int row = categoryService.update(category);

        if (row > 0) {
            return R.success("更新成功!");
        } else {
            return R.error("更新失败！");
        }


    }

    //获取菜品分类
    @GetMapping({"/list/{type}", "/list"})
    public R findByType(@PathVariable(required = false) Integer type) {

        List<Category> categoryList = categoryService.findByType(type);

        return R.success(categoryList);
    }


}
